/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.dto.sqlServer.InBoundStation;
import me.zhengjie.service.InBoundStationService;
import me.zhengjie.service.mapper.InBoundStationMapper;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.InBoundStationRepository;
import me.zhengjie.service.dto.InBoundStationDto;
import me.zhengjie.service.dto.InBoundStationQueryCriteria;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author ZhangYY
* @date 2024-01-19
**/
@Service
@RequiredArgsConstructor
public class InBoundStationServiceImpl implements InBoundStationService {

    private final InBoundStationRepository inBoundStationRepository;
    private final InBoundStationMapper inBoundStationMapper;

    @Override
    public PageResult<InBoundStationDto> queryAll(InBoundStationQueryCriteria criteria, Pageable pageable){
        Page<InBoundStation> page = inBoundStationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(inBoundStationMapper::toDto));
    }

    @Override
    public List<InBoundStationDto> queryAll(InBoundStationQueryCriteria criteria){
        return inBoundStationMapper.toDto(inBoundStationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public InBoundStationDto findById(Integer id) {
        InBoundStation inBoundStation = inBoundStationRepository.findById(id).orElseGet(InBoundStation::new);
        ValidationUtil.isNull(inBoundStation.getId(),"InBoundStation","id",id);
        return inBoundStationMapper.toDto(inBoundStation);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(InBoundStation resources) {
        inBoundStationRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(InBoundStation resources) {
        InBoundStation inBoundStation = inBoundStationRepository.findById(resources.getId()).orElseGet(InBoundStation::new);
        ValidationUtil.isNull( inBoundStation.getId(),"InBoundStation","id",resources.getId());
        inBoundStation.copy(resources);
        inBoundStationRepository.save(inBoundStation);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            inBoundStationRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<InBoundStationDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (InBoundStationDto inBoundStation : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" stationCode",  inBoundStation.getStationCode());
            map.put(" plcId",  inBoundStation.getPlcId());
            map.put("  reservoirAreaId",  inBoundStation.getReservoirAreaId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}